<!--
 * @Descripttion: (表 2 矿图数据项说明/Pro_miningmap)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2024-12-18)
 * @LastEditors: (admin)
 * @LastEditTime: (2024-12-18)
-->
<template>
  <div class="app-container">
    <!-- :model属性用于表单验证使用 比如下面的el-form-item 的 prop属性用于对表单值进行验证操作 -->
    <el-form :model="queryParams" size="small" label-position="right" inline ref="queryForm" :label-width="labelWidth"
      v-show="showSearch" @submit.native.prevent>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['prominingmap:add']" plain icon="el-icon-plus" size="mini"
          @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['prominingmap:edit']" plain icon="el-icon-edit"
          size="mini" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['prominingmap:delete']" plain icon="el-icon-delete"
          size="mini" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" border highlight-current-row @sort-change="sortChange"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column prop="sort" label="矿图排序" width="80" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="belongMine" label="煤矿编码" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="id" label="动态四色图编码" align="center" />
      <el-table-column prop="layerDetailName" label="矿图名称" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="layerType" label="矿图分类" align="center">
        <template slot-scope="scope">
          <dict-tag :options="layerTypeOptions" :value="scope.row.layerType" />
        </template>
      </el-table-column>

      <el-table-column prop="extent" label="切片范围" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="fileName" label="文件名" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link :href="scope.row.fileUrl" type="primary">{{ scope.row.fileName }}</el-link>
          <!-- <a :href="scope.row.fileUrl" download>{{ scope.row.fileName }}</a> -->
        </template>

      </el-table-column>
      <el-table-column prop="scale" label="比例尺" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="isSlice" label="是否需要切图" align="center">
        <template slot-scope="scope">
          <dict-tag :options="isSliceOptions" :value="scope.row.isSlice" />
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="创建时间" align="center" :show-overflow-tooltip="true" />
      <!-- <el-table-column prop="fileUrl" label="文件路径" align="center">
        <template slot-scope="scope">
          <el-image class="table-td-thumb" fit="contain" :src="scope.row.fileUrl"
            :preview-src-list="[scope.row.fileUrl]">
            <div slot="error"><i class="el-icon-document" /></div>
          </el-image>
        </template>
      </el-table-column> -->
      <!-- <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true" /> -->
      <el-table-column label="操作" align="center" width="140">
        <template slot-scope="scope">
          <el-button size="mini" type="success" icon="el-icon-edit" title="编辑"
            @click="handleUpdate(scope.row)"></el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" title="删除"
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination class="mt10" background :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改表 2 矿图数据项说明对话框 -->
    <el-dialog :title="title" :lock-scroll="false" :visible.sync="open" width="700px">
      <el-form ref="form" :model="form" :rules="rules" :label-width="formLabelWidth">
        <el-row>

          <el-col :span="12">
            <el-form-item label="煤矿编码" prop="belongMine">
              <el-input style="width: 200px;" :disabled="true" v-model="form.belongMine" placeholder="请输入煤矿编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="矿图名称" prop="layerDetailName">
              <el-input style="width: 200px;" v-model="form.layerDetailName" placeholder="请输入矿图名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="矿图分类" prop="layerType">
              <el-select style="width: 200px;" v-model="form.layerType" placeholder="请选择矿图分类">
                <el-option v-for="item in layerTypeOptions" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="矿图排序" prop="sort">
              <el-input-number style="width: 200px;" v-model="form.sort" :min="1" :max="10000"
                label="请输入矿图排序"></el-input-number>
              <!-- <el-input style="width: 200px;" v-model="form.sort" placeholder="请输入矿图排序" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="切片范围" prop="extent">
              <el-input style="width: 200px;" v-model="form.extent" placeholder="请输入切片范围" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="比例尺" prop="scale">
              <el-input style="width: 200px;" v-model="form.scale" placeholder="请输入比例尺" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否需要切图" prop="isSlice">
              <el-radio-group v-model="form.isSlice">
                <el-radio v-for="item in isSliceOptions" :key="item.dictValue" :label="parseInt(item.dictValue)">{{
      item.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input style="width: 532px;" v-model="form.remark" placeholder="请输入备注" type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="文件路径" prop="fileUrls">
              <el-upload :file-list="form.fileUrls" :action="fileUrl" :limit="3" :on-exceed="handleExceed"
                :before-upload="(file) => {
      return handleBeforeUpload(file)
    }
      " accept=".jpg,.jpeg,.png,.pdf,.JPG,.JPEG,.PNG,.PDF">
                <el-button size="small" type="primary">上传</el-button>
                <div slot="tip" class="el-upload__tip">可选择文件上传，支持jpg、jpeg、png、pdf格式，单个文件最大不超过5MB</div>
              </el-upload>
            </el-form-item>
          </el-col>





        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import {
  listProMiningmap,
  addProMiningmap,
  delProMiningmap,
  updateProMiningmap,
  getProMiningmap,
} from '@/api/project/proMiningmap.js';
import $ from 'jquery'
export default {
  name: "prominingmap",
  data() {
    return {
      fileUrl: "",
      labelWidth: "100px",
      formLabelWidth: "100px",
      // 选中id数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: undefined,
        sortType: undefined,
      },
      // 弹出层标题
      title: "",
      // 操作类型 1、add 2、edit
      opertype: 0,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        fileUrls: [],
      },

      // 矿图分类选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      layerTypeOptions: [],
      // 数据操作标识选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      stateFlagOptions: [],
      // 是否删除选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      isDeleteOptions: [],
      // 是否需要切图选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      isSliceOptions: [{ dictLabel: '是', dictValue: '1' }, { dictLabel: '否', dictValue: '0' }],
      // 数据列表
      dataList: [],
      // 总记录数
      total: 0,
      // 提交按钮是否显示
      btnSubmitVisible: true,
      // 表单校验
      rules: {

        layerDetailName: [
          { required: true, message: '请输入矿图名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],

        layerType: [
          { required: true, message: '请选择矿图分类', trigger: 'change' },
        ],

        extent: [
          { required: true, message: '请输入切片范围', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        scale: [
          { required: true, message: '请输入比例尺', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],

        isSlice: [
          { required: true, message: '请选择是否切图', trigger: 'change' },
        ],
        sort: [
          { required: true, message: '请输入矿图排序', trigger: 'blur' },
        ],
       
      },
    };
  },
  created() {
    // 列表数据查询
    this.getList();

    this.getDicts('sys_kttype').then((response) => {
      this.layerTypeOptions = response.data
    })
  },
  methods: {
    handleExceed() {
      this.$message.error('上传文件不能超过3个')
    },
    handleViewFile(file) {
      let a = document.createElement('a')
      a.href = file.path
      a.download = file.name
      a.click()
    },
    onRemove(file, type) {
      if (type == '1') {
        let index = this.form.JsFiles.findIndex((fileItem) => fileItem.name === file.name)
        this.form.JsFiles.splice(index, 1)
        console.log(this.form.JsFiles)
      }
      if (type == '2') {
        let index = this.form.HtFiles.findIndex((fileItem) => fileItem.name === file.name)
        this.form.HtFiles.splice(index, 1)
        console.log(this.form.HtFiles)
      }
    },
    handleBeforeUpload(file) {
      this.uploadFile(file)
      return false
    },
    //图片上传
    uploadFile(file) {
      debugger;
      var _this = this
      if (file.size >= 50 * 1024 * 1024) {
        this.$message.error('上传文件大小不能超过50MB')
        return false
      }
      var num = file.name.match(/[.]/g).length
      const extension1 = file.name.split('.')[num].toLowerCase() === 'jpg'
      const extension2 = file.name.split('.')[num].toLowerCase() === 'png'
      const extension4 = file.name.split('.')[num].toLowerCase() === 'jpeg'
      const extension5 = file.name.split('.')[num].toLowerCase() === 'JPG'
      const extension6 = file.name.split('.')[num].toLowerCase() === 'JPEG'
      const extension7 = file.name.split('.')[num].toLowerCase() === 'PNG'
      const extension8 = file.name.split('.')[num].toLowerCase() === 'pdf'
      const extension9 = file.name.split('.')[num].toLowerCase() === 'PDF'
      const extension10 = file.name.split('.')[num].toLowerCase() === 'DOCX'
      const extension11 = file.name.split('.')[num].toLowerCase() === 'docx'
      if (!extension10 && !extension10 && !extension1 && !extension2 && !extension4 && !extension5 && !extension6 && !extension8 && !extension9 && !extension7) {
        this.$message.error('请上传正确格式，支持jpg、jpeg、png、pdf、docx格式。')
        return false
      }
      var formdata = new FormData()
      formdata.append('file', file)
      formdata.append('showUrl', this.setting.fileurl)
      formdata.append('path', '')
      formdata.append('scene', 'default')
      var settings = {
        url: this.setting.fileurl + '/group1/upload',
        method: 'POST',
        timeout: 0,
        processData: false,
        mimeType: 'multipart/form-data',
        contentType: false,
        data: formdata,
      }
      $.ajax(settings).done(function (response) {
        if (response != null) {
          if (file.name.length > 36) {
            _this.$message.error('文件名称不能超过36个字符！')
            return false
          }
          _this.form.fileUrl = response;
          _this.form.fileName = file.name
          _this.form.fileUrls.push({
            name: file.name,
            Path: response,
            url: response,
          })
        } else {
          // _this.$message.error("文件上传失败");
        }
      })
    },
    // 查询数据
    getList() {
      this.loading = true;
      listProMiningmap(this.queryParams).then(res => {
        if (res.code == 200) {
          this.dataList = res.data.result;
          this.total = res.data.totalNum;
          this.loading = false;
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 重置数据表单
    reset() {
      this.form = {
        belongMine: undefined,
        uuid: undefined,
        id: undefined,
        layerDetailName: undefined,
        layerType: undefined,
        sort: undefined,
        extent: undefined,
        fileName: undefined,
        scale: undefined,
        createDate: undefined,
        updateDate: undefined,
        stateFlag: undefined,
        fileUrl: "",
        fileUrls: [],
        remark: undefined,
        isDelete: undefined,
        isSlice: undefined,
      };
      this.resetForm("form");
    },
    // 重置查询操作
    resetQuery() {
      this.timeRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1
      this.multiple = !selection.length;
    },
    // 自定义排序
    sortChange(column) {
      if (column.prop == null || column.order == null) {
        this.queryParams.sort = undefined;
        this.queryParams.sortType = undefined;
      } else {
        this.queryParams.sort = column.prop;
        this.queryParams.sortType = column.order;
      }

      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getbelongMine().then(res => {
        if (res.code == 210) {
          this.$message({
            message: '请先在企业信息管理中添加煤矿编码',
            type: 'warning'
          });
        } else {
          this.reset();
          this.form.belongMine = res.data.sysCode;
          this.open = true;
          this.title = "添加";
          this.opertype = 1;

        }
      });


    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.id || this.ids;

      this.$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
        .then(function () {
          return delProMiningmap(Ids);
        })
        .then(() => {
          this.handleQuery();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProMiningmap(id).then((res) => {
        const { code, data } = res;
        if (code == 200) {
          this.open = true;
          this.title = "修改数据";
          this.opertype = 2;

          this.form = {
            ...data,
          };
        }
      });
    },
    //图片上传成功方法
    handleUploadSuccess(column, filelist) {
      this.form[column] = filelist;
    },
    // 矿图分类字典翻译
    layerTypeFormat(row, column) {
      return this.selectDictLabel(this.layerTypeOptions, row.layerType);
    },
    // 数据操作标识字典翻译
    stateFlagFormat(row, column) {
      return this.selectDictLabel(this.stateFlagOptions, row.stateFlag);
    },
    // 是否删除字典翻译
    isDeleteFormat(row, column) {
      return this.selectDictLabel(this.isDeleteOptions, row.isDelete);
    },
    // 是否需要切图字典翻译
    isSliceFormat(row, column) {
      return this.selectDictLabel(this.isSliceOptions, row.isSlice);
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(JSON.stringify(this.form));

          if (this.form.id != undefined && this.opertype === 2) {
            updateProMiningmap(this.form)
              .then((res) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((err) => {
                //TODO 错误逻辑
              });
          } else {
            addProMiningmap(this.form)
              .then((res) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((err) => {
                //TODO 错误逻辑
              });
          }
        }
      });
    },
  },
};
</script>